﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Serialization
{
    [Serializable]
    public class Person
    {
        public int Age { get; private set; }
        public string Name { get; private set; }
        public Person(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
    }

    class Program
    {

        static void Main(string[] args)
        {
            Person alice = new Person("alice", 20);

            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter
                = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            formatter.Serialize(ms, alice);

            byte[] content = ms.ToArray();
            Console.WriteLine(BitConverter.ToString(content));
            Console.WriteLine();
            string base64 = Convert.ToBase64String(content);
            Console.WriteLine(base64);
            Console.WriteLine();

            byte[] content2 = Convert.FromBase64String(base64);
            System.IO.MemoryStream ms2 = new System.IO.MemoryStream(content);
            Person alice2 = formatter.Deserialize(ms2) as Person;
            Console.WriteLine(alice2.Name);
        }
    }
}
